// 指针和数组面试题

#include <cstdio>
#include <cstring>

int main()
{
    int a[] = {1, 2, 3, 4};
    printf("%d\n", sizeof(a));         // sizeof(数组名)计算整个数组大小 -> 16字节
    printf("%d\n", sizeof(a + 0));     // sizeof(首元素地址) -> 4/8字节
    printf("%d\n", sizeof(*a));        // sizeof(int) -> 4字节
    printf("%d\n", sizeof(a + 1));     // sizeof(第二个元素地址) -> 4/8字节
    printf("%d\n", sizeof(a[1]));      // sizeof(int) -> 4字节
    printf("%d\n", sizeof(&a));        // sizeof(整个数组起始地址) -> 4/8字节
    printf("%d\n", sizeof(*&a));       // sizeof(a) -> 16字节
    printf("%d\n", sizeof(&a + 1));    // sizeof(数组末尾地址) -> 4/8字节
    printf("%d\n", sizeof(&a[0]));     // sizeof(首元素地址) -> 4/8字节
    printf("%d\n", sizeof(&a[0] + 1)); // sizeof(第二个元素地址) -> 4/8字节

    // ----------------------------------------------------------------------------------------

    char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};
    printf("%d\n", sizeof(arr));         // sizeof(数组名)计算整个数组大小 -> 6
    printf("%d\n", sizeof(arr + 0));     // sizeof(首元素地址) -> 4/8字节
    printf("%d\n", sizeof(*arr));        // sizeof(char) -> 1字节
    printf("%d\n", sizeof(arr[1]));      // sizeof(char) -> 1字节
    printf("%d\n", sizeof(&arr));        // sizeof(整个数组起始地址) -> 4/8字节
    printf("%d\n", sizeof(&arr + 1));    // sizeof(数组末尾地址) -> 4/8字节
    printf("%d\n", sizeof(&arr[0] + 1)); // sizeof(第二个元素地址) -> 4/8字节
    printf("%d\n", strlen(arr));         // strlen遇到'\0'结束 -> 随机值
    printf("%d\n", strlen(arr + 0));     // 随机值
    printf("%d\n", strlen(*arr));        // err
    printf("%d\n", strlen(arr[1]));      // err
    printf("%d\n", strlen(&arr));        // 随机值(&arr拿到整个数组的起始地址,但不知道'\0'在内存哪个位置)
    printf("%d\n", strlen(&arr + 1));    // 随机值
    printf("%d\n", strlen(&arr[0] + 1)); // 随机值

    // ----------------------------------------------------------------------------------------

    char arr[] = "abcdef";
    printf("%d\n", sizeof(arr));         // sizeof(数组名) -> 7字节
    printf("%d\n", sizeof(arr + 0));     // sizeof(首元素地址) -> 4/8字节
    printf("%d\n", sizeof(*arr));        // sizeof(char) -> 1字节
    printf("%d\n", sizeof(arr[1]));      // sizeof(char) -> 1字节
    printf("%d\n", sizeof(&arr));        // sizeof(整个数组起始地址) -> 4/8字节
    printf("%d\n", sizeof(&arr + 1));    // sizeof(数组末尾地址) -> 4/8字节
    printf("%d\n", sizeof(&arr[0] + 1)); // sizeof(第二个元素地址) -> 4/8字节
    printf("%d\n", strlen(arr));         // 6字节
    printf("%d\n", strlen(arr + 0));     // 6字节
    printf("%d\n", strlen(*arr));        // err
    printf("%d\n", strlen(arr[1]));      // err
    printf("%d\n", strlen(&arr));        // 6字节
    printf("%d\n", strlen(&arr + 1));    // 随机值
    printf("%d\n", strlen(&arr[0] + 1)); // 5字节

    // ----------------------------------------------------------------------------------------

    char *p = "abcdef";
    printf("%d\n", sizeof(p));         // sizeof(char*) -> 4/8字节
    printf("%d\n", sizeof(p + 1));     // sizeof(char*) -> 4/8字节
    printf("%d\n", sizeof(*p));        // sizeof(char) -> 1字节
    printf("%d\n", sizeof(p[0]));      // sizeof(char) -> 1字节
    printf("%d\n", sizeof(&p));        // sizeof(char**) -> 4/8字节
    printf("%d\n", sizeof(&p + 1));    // sizeof(char**) -> 4/8字节
    printf("%d\n", sizeof(&p[0] + 1)); // sizeof(char*) -> 4/8字节
    printf("%d\n", strlen(p));         // 6字节
    printf("%d\n", strlen(p + 1));     // 5字节
    printf("%d\n", strlen(*p));        // err
    printf("%d\n", strlen(p[0]));      // err
    printf("%d\n", strlen(&p));        // 随机值
    printf("%d\n", strlen(&p + 1));    // 随机值
    printf("%d\n", strlen(&p[0] + 1)); // 5字节

    // ----------------------------------------------------------------------------------------

    int a[3][4] = {0};
    printf("%d\n", sizeof(a));            // sizeof(数组名)计算整个数组大小 -> 48字节
    printf("%d\n", sizeof(a[0][0]));      // sizeof(int) -> 4字节
    printf("%d\n", sizeof(a[0]));         // sizeof(a[0]) -> sizeof(第一行数组名) -> 16字节
    printf("%d\n", sizeof(a[0] + 1));     // sizeof(a[0][1]元素地址) -> 4/8字节
    printf("%d\n", sizeof(*(a[0] + 1)));  // sizeof(int) -> 4字节
    printf("%d\n", sizeof(a + 1));        // sizeof(int (*a)[4]) -> 4/8字节
    printf("%d\n", sizeof(*(a + 1)));     // sizeof(第二行数组名) -> 16字节
    printf("%d\n", sizeof(&a[0] + 1));    // sizeof(二维数组第二行地址) -> 4/8字节
    printf("%d\n", sizeof(*(&a[0] + 1))); // sizeof(a[1]) -> 16字节
    printf("%d\n", sizeof(*a));           // sizeof(a[0]) -> 16字节
    printf("%d\n", sizeof(a[3]));         // 16字节

    // ----------------------------------------------------------------------------------------

    return 0;
}

// ----------------------------------------------------------------------------------------

int main()
{
    int a[5] = {1, 2, 3, 4, 5};
    int *ptr = (int *)(&a + 1);
    printf("%d,%d", *(a + 1), *(ptr - 1));
    // 分析
    // *(a + 1) = a[1] = 2
    // ptr = 一维数组末尾地址
    // *(ptr - 1) = 5
    return 0;
}

// ----------------------------------------------------------------------------------------

struct Test
{
	int Num;
	char* pcName;
	short sDate;
	char cha[2];
	short sBa[4];
}*p;
//假设p 的值为0x100000。 如下表表达式的值分别为多少？
//已知，结构体Test类型的变量大小是20个字节
int main()  
{
	p = (struct Test*)0x100000;
	printf("%p\n", p + 0x1);
	printf("%p\n", (unsigned long)p + 0x1);
	printf("%p\n", (unsigned int*)p + 0x1);
	return 0;
}

// 分析
// struct Test* p = 0x100000
// p + 0x1 : 跳过一个struct Test大小 -> 0x100000 + 0x000014 = 0x100014
// (unsigned long)p + 0x1 -> 0x100000 + 0x1 = 0x100001
// (unsigned int*)p + 0x1 -> 0x100000 + 0x4 = 0x100004

// ----------------------------------------------------------------------------------------

int main()
{
	int a[4] = { 1, 2, 3, 4 };
	int* ptr1 = (int*)(&a + 1);
	int* ptr2 = (int*)((int)a + 1);
	printf("%x,%x", ptr1[-1], *ptr2);
	return 0;
}

// 分析
// ptr1 = 数组末尾地址
// ptr2 = 第一个元素第二个字节的地址
// ptr1[-1] = 4 *ptr2 = 20000000 

// ----------------------------------------------------------------------------------------

int main()
{
	int a[3][2] = { (0, 1), (2, 3), (4, 5) };
	int* p;
	p = a[0];
	printf("%d", p[0]);
	return 0;
}

// 分析
// p = a[0] 
// p[0] = a[0][0] = 1

// ----------------------------------------------------------------------------------------

int main()
{
	int a[5][5];
	int(*p)[4];
	p = a;
	printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
	return 0;
}

// 分析
// 0 0 0 0 0 
// 0 0 0 0 0 
// 0 0 0 0 0 
// 0 0 0 0 0 
// 0 0 0 0 0 

// &p[4][2] - &a[4][2] = -4
// -4 = 10000000 00000000 00000000 00000100(原码)
// -4 = 11111111 11111111 11111111 11111100(补码)
// 16进制 : FFFFFFFC

// ----------------------------------------------------------------------------------------

int main()
{
	int aa[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int* ptr1 = (int*)(&aa + 1);
	int* ptr2 = (int*)(*(aa + 1));
	printf("%d,%d", *(ptr1 - 1), *(ptr2 - 1));
	return 0;
}

// 分析
// ptr1 = 二维数组末尾地址
// ptr2 = 第二行起始地址
// *(ptr1 - 1) = 10 *(ptr2 - 1) = 5

// ----------------------------------------------------------------------------------------

int main()
{
	char* a[] = { "work","at","alibaba" };
	char** pa = a;
	pa++;
	printf("%s\n", *pa);
	return 0;
}

// 分析
// *pa = "at"

// ----------------------------------------------------------------------------------------

int main()
{
	char* c[] = { "ENTER","NEW","POINT","FIRST" };
	char** cp[] = { c + 3,c + 2,c + 1,c };
	char*** cpp = cp;
	printf("%s\n", **++cpp); // POINT
	printf("%s\n", *-- * ++cpp + 3); // ER
	printf("%s\n", *cpp[-2] + 3); //ST
	printf("%s\n", cpp[-1][-1] + 1); // EW
	return 0;
}

// ----------------------------------------------------------------------------------------
